package com.uzai.console.dto.report;

import com.alibaba.fastjson.JSONObject;
import com.uzai.console.dto.BaseDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @Auther: liuqi
 * @Date: 2021/8/10 14:58
 * @Description:
 */
@ApiModel("支出统计指标查询参数")
@Data
public class RealTimeOverviewReportDto extends BaseDto {
    @ApiModelProperty("设备类型(private_wechat：个人微信；work_wechat-企业微信；wx_pub-公众号)")
    private String deviceType;
    @ApiModelProperty("机器人ID列表")
    private List<Long> deviceUniqueIdList;
    @ApiModelProperty("查询开始时间")
    private Integer queryStartTime;
    @ApiModelProperty("查询结束时间")
    private Integer queryEndTime;
    @ApiModelProperty("平台(0-全部；1-淘宝；2-京东；3-拼多多；4-唯品会；5-抖音；6-美团；7-饿了么；8-快手)")
    private Integer platformType;

    /**
     * 请求
     * @author jitwxs
     * @date 2022/1/16 17:19
     * @return java.lang.String
     */
    public String signTopRequest() {
        Map<String, String> params = new HashMap<>();
        params.put("merId", String.valueOf(getMerId()));
        params.put("deviceType", deviceType);
        //机器人列表
        List<Long> subDeviceUniqueIdList = getSubDeviceUniqueIdList();
        //子账号不为空，则优先子账号
        if(subDeviceUniqueIdList != null && subDeviceUniqueIdList.size() > 0){
            Collections.sort(subDeviceUniqueIdList);
            params.put("subDeviceUniqueIdList", JSONObject.toJSONString(subDeviceUniqueIdList));
        }else{
            if(deviceUniqueIdList != null && deviceUniqueIdList.size() > 0){
                Collections.sort(deviceUniqueIdList);
                params.put("deviceUniqueIdList", JSONObject.toJSONString(deviceUniqueIdList));
            }
        }
        params.put("queryStartTime", String.valueOf(queryStartTime));
        params.put("queryEndTime", String.valueOf(queryEndTime));
        params.put("platformType", String.valueOf(platformType));
        // 第一步：检查参数是否已经排序
        String[] keys = params.keySet().toArray(new String[0]);
        Arrays.sort(keys);

        // 第二步：把所有参数名和参数值串在一起
        StringBuilder query = new StringBuilder();
        for (String key : keys) {
            String value = params.get(key);
            if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(value)) {
                query.append(key).append(value);
            }
        }
        // 第三步：使用MD5加密
        return DigestUtils.md5Hex(query.toString());
    }

}
